﻿using DevExpress.XtraEditors;
using DLL;
using SQLDLL;
using System;
using System.Data;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace NursingFormSystem
{
    public partial class SetEduDepartment : Form
    {
        public string choose = string.Empty;
        public SetEduDepartment(string departmentlist)
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            Text = "选择病区";
            string yuanqu = @"SELECT DepartmentName,DepartmentCode  FROM [NursingInfoSystem].[dbo].[tb_Department](NOLOCK) where Location like '%新院区%'";
            DataTable yuanqutable = SqlHelper.ExecuteDataset(yuanqu, CommandType.Text).Tables[0];
            if (yuanqutable.Rows.Count == 0)
            {
                XtraMessageBox.Show("未找到病区！");
                Close();
            }
            string[] DepartmentName = new string[yuanqutable.Rows.Count];
            string[] DepartmentCode = new string[yuanqutable.Rows.Count];
            for (int i = 0; i < yuanqutable.Rows.Count; i++)
            {
                DepartmentName[i] = yuanqutable.Rows[i][0].ToString();
                DepartmentCode[i] = yuanqutable.Rows[i][1].ToString().Trim();
            }
            int j = 0;
            int k = 0;
            for (int i = 0; i < yuanqutable.Rows.Count; i++)
            {
                CheckEdit chkb = new CheckEdit();
                chkb.Name = string.Format(i.ToString());
                chkb.Text = string.Format(DepartmentName[i]);
                chkb.Tag = string.Format(DepartmentCode[i]);
                chkb.Padding = new Padding(0, 0, 5, 0);
                if (j == 0)  //第一个
                {
                    chkb.Location = new Point(20 + 120 * j, 8 + 21 * k);
                }
                if (j == 1) //222
                {
                    chkb.Location = new Point(20 + 120 * j, 8 + 21 * k);
                }
                if (j == 2)//333
                {
                    chkb.Location = new Point(20 + 120 * j, 8 + 21 * k);
                }
                if (j == 3)//444
                {
                    chkb.Location = new Point(20 + 120 * j, 8 + 21 * k);
                }

                chkb.AutoSize = true;
                chkb.Width = 120;
                Controls.Add(chkb);
                if (j == 3)
                {
                    j = 0;
                    k++;
                }
                else j++;
            }
            SimpleButton buttonall = new SimpleButton();
            buttonall.Click += buttonall_Click;
            buttonall.Location = new Point(20, 8 + 21 * (k + 2));
            buttonall.Text = "全选";
            Controls.Add(buttonall);
            SimpleButton buttonnotall = new SimpleButton();
            buttonnotall.Click += buttonnotall_Click;
            buttonnotall.Location = new Point(100, 8 + 21 * (k + 2));
            buttonnotall.Text = "取消全选";
            Controls.Add(buttonnotall);

            SimpleButton buttonok = new SimpleButton();
            buttonok.Click += buttonok_Click;
            buttonok.Location = new Point(200, 8 + 21 * (k + 2));
            buttonok.Text = "确定";
            Controls.Add(buttonok);

            SimpleButton buttoncancel = new SimpleButton();
            buttoncancel.Click += buttoncancel_Click;
            buttoncancel.Location = new Point(300, 8 + 21 * (k + 2));
            buttoncancel.Text = "取消";
            Controls.Add(buttoncancel);
            Height = 8 + 21 * (k + 6);
            Width = 20 + 510;

            if (departmentlist != null)  //已选择过
            {
                string[] temp = Regex.Split(departmentlist, ",", RegexOptions.IgnoreCase);
                for (int i = 0; i < temp.Length; i++)
                {
                    for (int p = 0; p < Controls.Count - 4; p++)
                    {
                        if (temp[i] == ((CheckEdit)(Controls[p.ToString()])).Tag.ToString())
                        {
                            ((CheckEdit)(Controls[p.ToString()])).Checked = true;
                        }
                    }
                }
            }
        }

        private void buttoncancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonok_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < Controls.Count - 4; i++)
            {
                if (((CheckEdit)(Controls[i.ToString()])).Checked)
                {
                    choose += ((CheckEdit)(Controls[i.ToString()])).Tag;
                    choose += ",";
                }
            }
            if (choose.Length == 0)
            {
                DialogResult = DialogResult.No;
                Close();
            }
            else
            {
                choose = choose.Substring(0, choose.Length - 1);
                DialogResult = DialogResult.OK;
                Close();
            }
        }

        private void buttonnotall_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < Controls.Count - 4; i++)
            {
                ((CheckEdit)(Controls[i.ToString()])).Checked = false;
            }
        }

        private void buttonall_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < Controls.Count - 4; i++)
            {
                ((CheckEdit)(Controls[i.ToString()])).Checked = true;
            }
        }
    }
}
